#include "device.h"
#include <iostream>

// by Diego Oriato
int main(int argc, char * argv[]) {

  int i;
  int num_min;
  
  double ** c1v1,
		 polar_ratio,
		 end_polar_ratio;
  
  Device * cart;
  
  FILE * fp;

  /*scanf_s("%d",&argc);*/

  switch(argc) {
  case 1:
    cart = new Device(0.0,0.0);
    cart->simulator();
    c1v1 = cart->optical_transitions(num_min);
	for(i=0;i<num_min;i++) {
		printf("Perc=0  E=%lf(eV)   lambda= %lf(nm)   overlap= %le  field= %le\n",
				  c1v1[0][i],
				  1241.54/c1v1[0][i],
				  c1v1[1][i],
				  cart->field_in_qw());
	}
    if(c1v1) {
      delete [] c1v1[0];
      delete [] c1v1[1];
      delete [] c1v1;
    }
    delete cart;
    break;
  case 2:
    polar_ratio = atof(argv[1]);
    if(polar_ratio<0.0 || polar_ratio>100.0 ) nrerror("Main: invalid argument passed!");
    cart = new Device(polar_ratio/100.0,polar_ratio/100.0);
    cart->simulator();
    c1v1 = cart->optical_transitions(num_min);
    for(i=0;i<num_min;i++) printf("Perc=%lf  E=%lf(eV)   lambda= %lf(nm)   overlap= %le  field= %le\n",
				  polar_ratio,c1v1[0][i],1241.54/c1v1[0][i],c1v1[1][i],cart->field_in_qw());
    if(c1v1) {
      delete [] c1v1[0];
      delete [] c1v1[1];
      delete [] c1v1;
    }
    delete cart;
    break;
  case 3:
    polar_ratio = atof(argv[1]);
    if(polar_ratio<0.0 || polar_ratio>100.0 ) nrerror("Main: invalid argument passed!");
    fopen_s(&fp,argv[2],"w");
    fclose(fp);
    while(polar_ratio<=100.0) {
      cart = new Device(polar_ratio/100.0,polar_ratio/100.0);
      cart->simulator();
      printf("--->Optical Transitions...\n");
      c1v1 = cart->optical_transitions(num_min);
      fopen_s(&fp,argv[2],"a");
      for(i=0;i<num_min;i++) {
	fprintf(fp,"%lf    %lf    %lf    %le   %le\n",
		polar_ratio,c1v1[0][i],1241.54/c1v1[0][i],c1v1[1][i],cart->field_in_qw());
	printf("%lf    E=%lf(eV)   lambda= %lf(nm)   overlap= %le  field= %le\n",
	       polar_ratio,c1v1[0][i],1241.54/c1v1[0][i],c1v1[1][i],cart->field_in_qw());
      }
      fclose(fp);
      if(c1v1) {
	delete [] c1v1[0];
	delete [] c1v1[1];
	delete [] c1v1;
      }
      delete cart;
      polar_ratio += 0.01;
    }
  case 4:
    polar_ratio = atof(argv[1]);
	if(polar_ratio<0.0 || polar_ratio>100.0 ) {
		nrerror("Main: invalid argument passed!");
	}
    end_polar_ratio = atof(argv[2]);
	if(end_polar_ratio<0.0 || end_polar_ratio>100.0 ) {
		nrerror("Main: invalid argument passed!");
	}
    
	fopen_s(&fp,argv[3],"w");
    fclose(fp);
    
	while(polar_ratio<=end_polar_ratio) {
      cart = new Device(polar_ratio/100.0,polar_ratio/100.0);
      cart->simulator();
      printf("--->Optical Transitions...\n");
      c1v1 = cart->optical_transitions(num_min);
      fopen_s(&fp,argv[3],"a");
      for(i=0;i<num_min;i++) {
		fprintf(fp,"%lf    %lf    %lf    %le   %le\n",
			polar_ratio,
			c1v1[0][i],
			1241.54/c1v1[0][i],
			c1v1[1][i],
			cart->field_in_qw());
		printf("%lf    E=%lf(eV)   lambda= %lf(nm)   overlap= %le  field= %le\n",
	       polar_ratio,
		   c1v1[0][i],
		   1241.54/c1v1[0][i],
		   c1v1[1][i],
		   cart->field_in_qw());
      }
      fclose(fp);
      if(c1v1) {
		delete [] c1v1[0];
		delete [] c1v1[1];
		delete [] c1v1;
      }
      delete cart;
      polar_ratio += 1.0;
    }

  }
    
  return 0;

}
